﻿using Furion.FriendlyException;
using Radius.NET.Application.Const;
using Radius.NET.Application.Entity;
using Radius.NET.Application.Service.ApiList.Dto;
using Radius.NET.Core.Service;
using System.ComponentModel;

namespace Radius.NET.Application.Service.ApiList
{
    /// <summary>
    /// 第三方接口服务
    /// </summary>
    [ApiDescriptionSettings(ApplicationConst.GroupName, Order = 002)]
    public class ApiConfigListService : BaseService<SysApiConfigList>,ITransient
    {
        private readonly SqlSugarRepository<SysApiConfigList> _apiConfigRep;
        private readonly SysCacheService _sysCacheService;
        public ApiConfigListService(SqlSugarRepository<SysApiConfigList> apiConfigRep,
            SysCacheService sysCacheService):base(apiConfigRep)
        {
            _apiConfigRep = apiConfigRep;
            _sysCacheService = sysCacheService;
        }

        /// <summary>
        /// 根据编码获取接口配置
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public async Task<SysApiConfigList> GetApiConfigByCode(string code)
        {
            var list = _sysCacheService.Get<List<SysApiConfigList>>(CacheConst.KeyApiConfig);
            if (list == null)
            {
                list = await _apiConfigRep.GetListAsync();
                _sysCacheService.Set(CacheConst.KeyApiConfig, list);
            }
            if (list.Find(x => x.Code.ToLower() == code.ToLower()) == null)
                throw Oops.Oh(ErrorCodeEnum.D1002);
            return list.Find(x => x.Code.ToLower() == code.ToLower());
        }

        /// <summary>
        /// 更新外部接口状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [ApiDescriptionSettings(Name = "changeStatus"), HttpPost]
        [DisplayName("更新外部接口状态")]
        public async Task ChangeApiConfigListStatus(UpdateApiConfigListStatusInput input)
        {
            var isExist = await _apiConfigRep.IsAnyAsync(u => u.Id == input.Id);
            if (!isExist)
                throw Oops.Oh(ErrorCodeEnum.D1002);

            var config = input.Adapt<SysApiConfigList>();
            await _apiConfigRep.AsUpdateable(config)
            .IgnoreColumns(true)
            .UpdateColumns(x => x.Status == config.Status)
            .ExecuteCommandAsync();
            _sysCacheService.Remove(CacheConst.KeyApiConfig);
        }

        /// <summary>
        /// 更新外部接口是否正式账套状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [ApiDescriptionSettings(Name = "changeIsMaster"), HttpPost]
        [DisplayName("更新外部接口是否正式账套状态")]
        public async Task ChangeApiConfigListIsMaster(UpdateApiConfigListIsMasterInput input)
        {
            var isExist = await _apiConfigRep.IsAnyAsync(u => u.Id == input.Id);
            if (!isExist)
                throw Oops.Oh(ErrorCodeEnum.D1002);

            var config = input.Adapt<SysApiConfigList>();
            await _apiConfigRep.AsUpdateable(config)
            .IgnoreColumns(true)
            .UpdateColumns(x => x.IsMaster == config.IsMaster)
            .ExecuteCommandAsync();
            _sysCacheService.Remove(CacheConst.KeyApiConfig);
        }

        /// <summary>
        /// 更新外部接口是否提交状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [ApiDescriptionSettings(Name = "changeIsSubmit"), HttpPost]
        [DisplayName("更新外部接口是否提交状态")]
        public async Task ChangeApiConfigListIsSubmit(UpdateApiConfigListIsSubmitInput input)
        {
            var isExist = await _apiConfigRep.IsAnyAsync(u => u.Id == input.Id);
            if (!isExist)
                throw Oops.Oh(ErrorCodeEnum.D1002);

            var config = input.Adapt<SysApiConfigList>();
            await _apiConfigRep.AsUpdateable(config)
            .IgnoreColumns(true)
            .UpdateColumns(x => x.IsSubmit == config.IsSubmit)
            .ExecuteCommandAsync();
            _sysCacheService.Remove(CacheConst.KeyApiConfig);
        }

        /// <summary>
        /// 更新外部接口是否审核状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [ApiDescriptionSettings(Name = "changeIsAudit"), HttpPost]
        [DisplayName("更新外部接口是否审核状态")]
        public async Task ChangeApiConfigListIsAudit(UpdateApiConfigListIsAuditInput input)
        {
            var isExist = await _apiConfigRep.IsAnyAsync(u => u.Id == input.Id);
            if (!isExist)
                throw Oops.Oh(ErrorCodeEnum.D1002);

            var config = input.Adapt<SysApiConfigList>();
            await _apiConfigRep.AsUpdateable(config)
            .IgnoreColumns(true)
            .UpdateColumns(x => x.IsAudit == config.IsAudit)
            .ExecuteCommandAsync();
            _sysCacheService.Remove(CacheConst.KeyApiConfig);
        }
    }
}
